package leading;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class ChinaCalender {
	public static void main(String[] args) {
		getClalender();
	}
	/**
	 * 用于得到日历
	 */
	public static void getClalender(){
		System.out.println("请输入年份：");
		Scanner sc = new Scanner(System.in);
		String year = sc.nextLine();
		while("".equals(year)||!year.matches("\\d*")||
				Integer.valueOf(year)>2030||Integer.valueOf(year)<1980){
			System.out.println("年输入有误,重新输入");
			year = sc.nextLine();
		}
		System.out.println("请输入月份：");
		String month = sc.nextLine();
		while("".equals(month)||!month.matches("\\d*")||
				Integer.valueOf(month)>12||Integer.valueOf(month)<1){
			System.out.println("月输入有误,重新输入");
			month = sc.nextLine();
		}
		
		GregorianCalendar gc = new GregorianCalendar();
		gc.set(Calendar.YEAR, Integer.valueOf(year));
		gc.set(Calendar.MONTH, Integer.valueOf(month)-1);
		gc.set(Calendar.DAY_OF_MONTH, 1);
		
		//用于判断是不是当月的当天时间
		GregorianCalendar tGc = new GregorianCalendar();
		tGc.setTime(new Date());
		int tYear = tGc.get(Calendar.YEAR);
		int tMonth = tGc.get(Calendar.MONTH);
		int tDay = tGc.get(Calendar.DAY_OF_MONTH);
//		Date d = gc.getTime();
//		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		System.out.println(sf.format(d));
//		System.out.println(gc.get(Calendar.DAY_OF_WEEK));
		int firstDay = 1;
//		System.out.println(gc.get(Calendar.DAY_OF_MONTH));
		System.out.println("该月的中国日历是：");
		System.out.println("\t"+"日"+"\t"+"一"+"\t"+"二"+"\t"+
							"三"+"\t"+"四"+"\t"+"五"+"\t"+"六");
		//while里的判断,如果是当月的话是一直打印，直到把当月的全打完,
		//判断月份是不是用户输入的月分,只要是则继续
		while(gc.get(Calendar.MONTH)==Integer.valueOf(month)-1){
			int week = gc.get(Calendar.DAY_OF_WEEK);
			int day = gc.get(Calendar.DAY_OF_MONTH);
			//打印这个月的第一天,for循环里是用来把第一天放到相应的星期上面
			if(firstDay==1){
				for(int i=1;i<=week;i++){
					System.out.print("\t");
				}
				//这里的判断是用来判断这个月第一天是不是今天,如果是要加*号
				if(tMonth==gc.get(Calendar.MONTH)&&tDay==day&&tYear==Integer.valueOf(year)){
					System.out.print(day);
					System.out.print("*");
				}else{
					System.out.print(day);
				}
				firstDay =2;
				//打印好一天后，要把相应的日期天数加1
				gc.add(Calendar.DAY_OF_MONTH, 1);
				continue;
			}
			//第一天打好了后,只要把后面的天数打印到相应的星期上就行,当这天是星期六时我们打
			//完这天就应该换行,
			if(week!=7){
				if(tMonth==gc.get(Calendar.MONTH)&&tDay==day&&tYear==Integer.valueOf(year)){
					System.out.print("\t"+day);
					System.out.print("*");
				}else{
					System.out.print("\t"+day);
				}
				gc.add(Calendar.DAY_OF_MONTH, 1);
				continue;
			}else{
				if(tMonth==gc.get(Calendar.MONTH)&&tDay==day&&tYear==Integer.valueOf(year)){
					System.out.println("\t"+day);//打完星期六就换行
					System.out.print("*");
				}else{
					System.out.println("\t"+day);
				}
				gc.add(Calendar.DAY_OF_MONTH, 1);
				continue;
			}
				
		}
	}
}
